package com.iscas.tech.service.impl;

import com.iscas.tech.bo.MdType;
import com.iscas.tech.entity.AtMdbusyValue;
import com.iscas.tech.entity.AtScitegach;
import com.iscas.tech.entity.AtStpic;
import com.iscas.tech.mapper.AtScitegachMapper;
import com.iscas.tech.param.PageParam;
import com.iscas.tech.service.AtMdbusyValueService;
import com.iscas.tech.service.AtScitegachService;
import com.iscas.tech.service.AtStpicService;
import com.iscas.tech.utils.DataUtil;
import com.iscas.tech.vo.AtScitegachDetailQueryVo;
import com.iscas.tech.vo.AtScitegachQueryVo;
import com.iscas.tech.vo.MdWithTechGain;
import io.geekidea.springbootplus.framework.common.service.impl.BaseServiceImpl;
import io.geekidea.springbootplus.framework.pagination.Paging;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 科技成果表 服务实现类
 *
 * @author yangyi
 * @since 2020-10-26
 */
@Slf4j
@Service
public class AtScitegachServiceImpl extends BaseServiceImpl<AtScitegachMapper, AtScitegach> implements AtScitegachService {

    @Autowired
    private AtScitegachMapper atScitegachMapper;

    @Autowired
    private AtStpicService mPicService;

    @Autowired
    private AtMdbusyValueService mBusinessService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveTechGain(MdWithTechGain param) throws Exception {
        if (atScitegachMapper.saveTechGain(param)) {
            savePicAndMd(param);
            return true;
        }
        return false;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateAtScitegach(MdWithTechGain param) throws Exception {
        AtScitegach techGain = param.transform();
        //  先修改基础信息
        if (!super.updateById(techGain)) {
            throw new Exception();
        }
        //  再删除关联图片信息
        mPicService.deleteByGainId(techGain.getId());
        //  再删除业务模块信息
        mBusinessService.deleteByBsId(techGain.getId().longValue(), MdType.TECH_GAIN.getType());
        //  新增关联图片信息、新增业务模块信息
        savePicAndMd(param);
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteTechGain(String ids) throws Exception {
        if (ids == null) {
            return true;
        }
        String[] idList = ids.split(",");
        for (String id : idList) {
            if (!atScitegachMapper.deleteTechGain(Long.parseLong(id))) {
                throw new Exception();
            }
        }
        return true;
    }

    @Override
    public Paging<AtScitegachQueryVo> getTechGain(PageParam pageParam, Integer audit) throws Exception {
        List<AtScitegachQueryVo> pageList = atScitegachMapper.getTechGain(pageParam.getOffset(), pageParam.getPageSize(), audit);
        Paging paging = new Paging();
        paging.setRecords(pageList);
        paging.setTotal(atScitegachMapper.getTechGainCount(audit));
        return paging;
    }

    @Override
    public AtScitegachDetailQueryVo get(Long id) {
        return atScitegachMapper.get(id);
    }

    @Override
    public Boolean checkTechGain(Long id) throws Exception {
        return atScitegachMapper.checkTechGain(id);
    }

    private void savePicAndMd(MdWithTechGain param) throws Exception {
        if (param.getPicList() != null) {
            for (AtStpic atStpic : DataUtil.getStPicList(param.getId(), param.getCreateTitle(), param.getPicList())) {
                if (!mPicService.saveAtStpic(atStpic)) {
                    throw new Exception();
                }
            }
        }
        if (param.getTagList() != null) {
            for (AtMdbusyValue business : DataUtil.getBusinessList(param.getId(), param.getCreateTitle(), param.getTagList())) {
                if (!mBusinessService.saveAtMdbusyValue(business)) {
                    throw new Exception();
                }
            }
        }
    }
}
